package com.jiu.ui;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import com.jiu.core.FishContext;

@SuppressWarnings("serial")
public class MyMenuBar extends JMenuBar {
	private MainFrame mainFrame;
	private FishContext context;
	public MyMenuBar(MainFrame mainFrame, FishContext context) {
		this.mainFrame = mainFrame;
		this.context = context;
		try {
			init();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	private void init() throws IOException {
		this.setBackground(new Color(200,200,255));
		
		this.add(createOptions());
		this.add(createGameHelp());
		this.add(createConfig());
		this.add(createOther());
	}

	private JMenu createOptions() throws IOException {
		JMenu m = new JMenu("选项");
		JMenuItem start = new JMenuItem("开始"); 
		JMenuItem stop = new JMenuItem("停止"); 
		JMenuItem qq = new JMenuItem("登录QQ"); 
		JMenuItem exit = new JMenuItem("退出"); 
		qq.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {				
				context.getQqThread().start();
			}
		});
		start.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				context.updateStatus(false);				
			}
		});
		stop.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				context.updateStatus(true);
			}
		});
		exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mainFrame.exit(mainFrame);
			}
		});
		m.add(start); 
		m.add(stop); 
		m.add(qq);
		m.add(exit);
		return m;
	}
	
	private JMenu createGameHelp() {
		JMenu m = new JMenu("游戏");
		JMenuItem bag = new JMenuItem("背包"); 
		JMenuItem me = new JMenuItem("技能"); 
		JMenuItem shop = new JMenuItem("拍卖场"); 
		bag.addActionListener(new ActionListener() {			
			public void actionPerformed(ActionEvent e) {
				if(isCatchPassword()){
					List<Map<String,Object>> listMsg = context.getFishHttpService().getFishBag();
					context.displayMessage(listMsg);					
				}
			}
		});
		me.addActionListener(new ActionListener() {			
			public void actionPerformed(ActionEvent e) {
				if(isCatchPassword()){
					List<Map<String,Object>> listMsg = context.getFishHttpService().getFishMyDetail();				
					context.displayMessage(listMsg);													
				}
			}
		});
		shop.addActionListener(new ActionListener() {			
			public void actionPerformed(ActionEvent e) {
				if(isCatchPassword()){
					int option = JOptionPane.showConfirmDialog(mainFrame, "是否显示全部拍卖信息？");
					List<Map<String,Object>> listMsg = context.getFishHttpService().getFishAuction(option==0?"0":"1");
					context.displayMessage(listMsg);					
				}
			}

		});
		m.add(bag);
		m.add(me);
		m.add(shop);
		return m;
	}
	private boolean isCatchPassword() {
		boolean flag = context.getAccount().getVerifyCode()!=null;
		if(!flag)JOptionPane.showMessageDialog(context.getMainFrame(), "未捕获到钓鱼客户端密码串，请先登录客户端！");
		return flag;
	}
	private JMenu createConfig() {
		JMenu m = new JMenu("设置");
		JMenuItem time = new JMenuItem("间隔时间");
		time.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text = JOptionPane.showInputDialog("请输入间隔时间，27秒代表不分解",context.getAccount().getTime());
				Pattern	pattern = Pattern.compile("\\d{2}");
				Matcher	matcher = pattern.matcher(text==null?"":text);
				if(matcher.find()){
					context.getAccount().setTime(Integer.valueOf(matcher.group()));
				}
			}
		});
		
		m.add(time);
		return m;
	}
	
	private JMenu createOther() throws IOException {
		JMenu m = new JMenu("其他");

		JMenuItem state = new JMenuItem("状态",new ImageIcon(ImageIO.read(getClass().getResourceAsStream("image/state.png"))));
		JMenuItem background = new JMenuItem("背景");
		JMenuItem help = new JMenuItem("帮助",new ImageIcon(ImageIO.read(getClass().getResourceAsStream("image/help.gif"))));
		state.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(mainFrame, context.getAccount().toString());
			}
		});
		background.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				context.getMainFrame().getTextPane().setOpaque(true);
				context.getMainFrame().getTextPane().setVisible(false);
				context.getMainFrame().getTextPane().setVisible(true);
			}
		});
		help.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(mainFrame, "笨蛋才需要帮助！");
			}
		});
		m.add(state);
		m.add(background);
		m.add(help);
		return m;
	}
	
}

